package com.dhl.fin.api.controller;

import com.dhl.fin.api.common.controller.CommonController;
import com.dhl.fin.api.common.dto.ApiResponse;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.domain.Tree;
import com.dhl.fin.api.service.TreeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author CuiJianbo
 * @date 2020.02.27
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("tree")
public class TreeController extends CommonController<Tree> {

    @Autowired
    private TreeServiceImpl treeService;

    @ResponseBody
    @RequestMapping("children")
    public ApiResponse children(HttpServletRequest request, Long[] treeIds, @RequestParam String pcode, @RequestParam String joinDomain) throws Exception {
        Tree tree = treeService.getByCode(pcode);
        List<Tree> children;
        children = treeService.getChildrenByTreeIds(tree.getId(), treeIds, joinDomain.split(","));
        return ApiResponse.success(children);
    }

    @ResponseBody
    @RequestMapping("root/{rootCode}")
    public ApiResponse getRoot(HttpServletRequest request, @PathVariable String rootCode, @RequestParam String joinDomain) throws Exception {
        Tree tree = treeService.getByCode(rootCode, joinDomain);
        return ObjectUtil.isNull(tree) ? ApiResponse.success(null) : ApiResponse.success(tree);
    }

    @ResponseBody
    @RequestMapping("changesort")
    public ApiResponse changeSort(HttpServletRequest request, int pos, String dragCode, String targetCode) throws Exception {
        treeService.changeSort(pos, dragCode, targetCode);
        return ApiResponse.success();
    }


}
